﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataProvider
{
    public static class DataProcessor
    {
        public static void Process(string file, Type type, List<SageField> fields, List<SageMethod> properties, List<SageMethod> methods, SageVersion version)
        {
            try
            {
                ProcessMethods(file, type, methods, version);
                ProcessProperties(file, type, properties, version);
                ProcessFields(file, type, fields, version);
            }
            catch { }
        }

        private static void ProcessFields(string file, Type type, List<SageField> fieldList, SageVersion version)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo info in fields)
            {
                // Igonore private internals
                if (info.IsPrivate && !info.IsFamilyAndAssembly) continue;
                try
                {
                    fieldList.Add(new SageField(file, info, version));
                }
                catch { }
            }
        }

        private static void ProcessProperties(string file, Type type, List<SageMethod> propertyList, SageVersion version)
        {
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (PropertyInfo info in properties)
            {
                // Only interested in the properties defined in this type
                if (type != info.DeclaringType) continue;
                // Igonore private internals
                if (info.GetOrSetMethod().IsPrivate && !info.GetOrSetMethod().IsFamilyAndAssembly) continue;
                try
                {
                    propertyList.Add(new SageMethod(file, info, version));
                }
                catch { }
            }
        }

        private static void ProcessMethods(string file, Type type, List<SageMethod> methodList, SageVersion version)
        {
            MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (MethodInfo info in methods)
            {
                // Ignore the get_ set_ methods representing properties
                if (info.IsSpecialName && (info.Name.StartsWith("set_") || info.Name.StartsWith("get_"))) continue;
                // Igonore private internals
                if (info.IsPrivate && !info.IsFamilyAndAssembly) continue;
                // Only interested in the methods defined in this type
                if (type != info.DeclaringType) continue;
                try
                {
                    methodList.Add(new SageMethod(file, info, version));
                }
                catch { }
            }
        }
    }
}
